package java_test.nettest;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

//服务器读取图片发送给客户端，客户端保存图片到本地
public class TCPTest7 {
    public static void main(String[] args) {
        cloent();
    }

    public static void cloent() {
        Socket socket = null;
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            socket = new Socket(InetAddress.getByName("127.0.0.1"), 8899);
            is = socket.getInputStream();
            fos = new FileOutputStream(new File("file\\image\\North1.jpg"));
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
